package com.Sdebruijn.curriculumvitae.models;

import java.util.Collection;

import android.os.Parcel;
import android.os.Parcelable;

import com.google.gson.annotations.SerializedName;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.field.ForeignCollectionField;
import com.j256.ormlite.table.DatabaseTable;

/**
 * Sub Class of {@link Person} representing a person's resume, containing
 * his/hers real-life info, online profiles, education and work experience
 * 
 * @author Stefan de Bruijn
 */
@DatabaseTable(tableName = "Applicant")
public class Applicant extends Person {

	Applicant() {
		// all persisted classes must define a no-arg constructor with at least
		// package visibility
	}

	/**
	 * {@link String} containing the {@link Person}'s nationality
	 */
	@DatabaseField
	@SerializedName("nationality")
	public String nationality;

	/**
	 * {@link String} containing the {@link Person}'s date of birth Json fails
	 * to provide a proper way to indicate a point in time, thus using a String
	 */
	@DatabaseField
	@SerializedName("date_of_birth")
	public String date_of_birth;

	/**
	 * {@link String} containing the {@link Person}'s postal address
	 */
	@DatabaseField
	@SerializedName("address")
	public String address; // TODO ORMLite/Gson don't support Address?

	/**
	 * {@link String} containing the {@link Person}'s current job or education
	 * title
	 */
	@DatabaseField
	@SerializedName("current_title")
	public String current_title;

	/**
	 * {@link String} containing the {@link Person}'s current company or school
	 */
	@DatabaseField
	@SerializedName("current_institution")
	public String current_institution;

	/**
	 * {@link Collection} of {@link App}s
	 */
	/*
	 * Tasks: Needs to be a Collection in order to be a child of
	 * ForeignCollection to work with ORMLite and Gson at the same time
	 */
	@ForeignCollectionField(eager = true)
	@SerializedName("apps")
	public Collection<App> apps;

	/**
	 * {@link Collection} of {@link Hobby}s
	 */
	/*
	 * Tasks: Needs to be a Collection in order to be a child of
	 * ForeignCollection to work with ORMLite and Gson at the same time
	 */
	@ForeignCollectionField(eager = true)
	@SerializedName("hobbies")
	public Collection<Hobby> hobbies;

	/**
	 * {@link Collection} of {@link Course}s
	 */
	/*
	 * Tasks: Needs to be a Collection in order to be a child of
	 * ForeignCollection to work with ORMLite and Gson at the same time
	 */
	@ForeignCollectionField(eager = true)
	@SerializedName("courses")
	public Collection<Course> courses;

	/*
	 * Required methods to have this class as Parcelable Having this class
	 * Parcelable will make it easier to restore it into fragments with
	 * savedInstanceState
	 */

	/*
	 * Just used as id - hashCode() will do fine
	 * 
	 * @see android.os.Parcelable#describeContents()
	 */
	@Override
	public int describeContents() {
		return this.hashCode();
	}

	/*
	 * Serialize my object attributes one by one
	 * 
	 * @see android.os.Parcelable#writeToParcel(android.os.Parcel, int)
	 */
	@Override
	public void writeToParcel(Parcel parcel, int flags) {
		parcel.writeInt(this.id);
		parcel.writeString(this.address);
		parcel.writeString(this.current_institution);
		parcel.writeString(this.current_title);
		parcel.writeString(this.date_of_birth);
		parcel.writeString(this.email);
		parcel.writeString(this.mobile);
		parcel.writeString(this.name);
		parcel.writeString(this.nationality);
		parcel.writeString(this.picture);
		parcel.writeParcelable(this.online_person, 0);
		// parcel.writeTypedList((List<App>) this.apps);
		// parcel.writeTypedList((List<Hobby>) this.hobbies);
	}

	/**
	 * Required for any Parcelable class to be recreated. Will be called to
	 * de-serialize the object.
	 * 
	 * @author Stefan de Bruijn
	 * 
	 */
	public static final Parcelable.Creator<Applicant> CREATOR = new Parcelable.Creator<Applicant>() {
		public Applicant createFromParcel(Parcel source) {
			return new Applicant(source);
		}

		public Applicant[] newArray(int size) {
			return new Applicant[size];
		}
	};

	/**
	 * <b>Do not use!</b> Private constructor for the Parcelable Creator. Will
	 * de-serialize the object.
	 * 
	 * @param source
	 */
	private Applicant(Parcel source) {
		this.id = source.readInt();
		this.address = source.readString();
		this.current_institution = source.readString();
		this.current_title = source.readString();
		this.date_of_birth = source.readString();
		this.email = source.readString();
		this.mobile = source.readString();
		this.name = source.readString();
		this.nationality = source.readString();
		this.picture = source.readString();

		this.online_person = source.readParcelable((ClassLoader) OnlinePerson.CREATOR);

		// source.readTypedList(((List<App>) this.apps), App.CREATOR);
		// source.readTypedList(((List<Hobby>) this.hobbies), Hobby.CREATOR);
	}
}
